﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using Argos.Data.Model;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using Argos.Windows.Forms;

namespace Argos.Design
{
	public class TestPropertiesTypeEditor : UITypeEditor
	{

		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			Test test = context.Instance as Test;
			if (null != test)
			{
				if (null != test.ActivityType)
				{
					Activity activity = (Activity)Activator.CreateInstance(test.ActivityType);
					if (null != value && value is IDictionary<string, object>)
					{
						Reflector.SetObjectProperties(activity, (IDictionary<string, object>)value);
					}

					using (PropertyEditorDialog dlg = new PropertyEditorDialog(activity))
					{
						if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
						{
							return dlg.GetProperties(Predicates.BindableBrowsableActivityPropertyMatcher);
						}
					}
				}
				else
				{
					throw new InvalidOperationException("Cannot edit the properties of a test before the activity type has been specified.");
				}
			}
			return value;
		}

		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}

	}
}
